package root.electronclass.electronclass.ext

import android.app.Activity
import android.content.Intent
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import root.electronclass.electronclass.common.base.BasicDialogFragment
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.common.util.ToastUtil
import root.electronclass.electronclass.CommonActivity
import root.electronclass.electronclass.mvp.GlobalParameter
import root.electronclass.electronclass.mvp.base.data
import root.electronclass.electronclass.mvp.base.isSuccessful
import root.electronclass.electronclass.mvp.rest.ClassApi
import root.electronclass.electronclass.grade.curriculum.ClassCurriculumSignDialog
import root.electronclass.electronclass.grade.homework.ClassHomeworkSubmitDialog
import root.electronclass.electronclass.grade.homework.ClassSubmitHomeworkFragment
import root.electronclass.electronclass.grade.model.ClassHomeworkItem
import root.electronclass.electronclass.grade.model.PersonType
import root.electronclass.electronclass.grade.rota.ClassManageRotaFragment
import root.electronclass.electronclass.other.AttestationActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *
 * @author Lokiy
 * @date 2023/4/1 15:02
 */

fun Fragment.onResult(requestCode: Int, resultCode: Int, data: Intent?) {
    val isSuccess = data?.getBooleanExtra("SUCCESS", false) ?: false
    if (resultCode == Activity.RESULT_OK && isSuccess) {
        lifecycleScope.launch(CoroutineExceptionHandler { }) {
            val result = withContext(Dispatchers.IO) { ClassApi.service.login() }
            if (result.isSuccessful) {
                GlobalParameter.setPersonLoginResult(result.data)
                val personType = result.data.personType
                if (personType == PersonType.UNKNOWN) {
                    return@launch ToastUtil.showToast(requireActivity(), "无操作权限")
                }

                //拿到学生的身份
                when (requestCode) {
                    AttestationActivity.HOMEWORK -> {//提交作业
                        data?.extras?.getParcelableArrayList<ClassHomeworkItem>(ViewBindingFragment.EXTRA_DATA)?.let {
                            ClassHomeworkSubmitDialog().apply {
                                arguments = bundleOf(
                                    BasicDialogFragment.TITLE to "提交作业",
                                    BasicDialogFragment.OK to "确定",
                                    BasicDialogFragment.CANCEL to "取消",
                                ).also { it.putAll(data.extras ?: bundleOf()) }
                            }.show(childFragmentManager, "CHSD")
                        } ?: run {
                            //没有作业的情况下
                            startFragment(ClassSubmitHomeworkFragment::class.java, bundleOf())
                        }
                    }
                    AttestationActivity.CURRICULUM -> {
                        if (personType == PersonType.STUDENT || personType == PersonType.CLASS_LEADER) {
                            return@launch ToastUtil.showToast(requireActivity(), "您不能执行此操作")
                        }
                        //签到 只有班干部和老师才能操作
                        ClassCurriculumSignDialog().apply {
                            arguments = bundleOf(
                                BasicDialogFragment.TITLE to "课程签到",
                                BasicDialogFragment.OK to "确定",
                                BasicDialogFragment.CANCEL to "取消",
                            ).also { it.putAll(data?.extras ?: bundleOf()) }
                        }.show(childFragmentManager, "CCSD")
                    }
                    AttestationActivity.MANAGER_ROTA -> {
                        if (personType == PersonType.STUDENT) {
                            return@launch ToastUtil.showToast(requireActivity(), "您不能执行此操作")
                        }
                        CommonActivity.startActivity(requireContext(), ClassManageRotaFragment::class.java)
                    }
                }
            } else {
                throw java.lang.IllegalArgumentException("获取身份信息失败")
            }
        }
    }

}